﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// IMG_BrightnessScreen class
    /// </summary>
    public partial class IMG_BrightnessScreen : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// Edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// track bar val
        /// </summary>
        private int trackBarVal = 0;

        /// <summary>
        /// Initializes a new instance of the IMG_BrightnessScreen class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_BrightnessScreen(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.picBoxOrig.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(img_original);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// open button clicked
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOrig.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.picBoxEdit.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Handles the Scroll event of the trackBar1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TrackBar1_Scroll(object sender, EventArgs e)
        {
            try
            {
                this.txtBoxTrackBarVal.Text = Convert.ToString(this.trackBarBrightness.Value);
                this.imgEdited = IMG_Processor.IMG_HistogramOp.IMG_BrightnessBuffer((this.imgEdited == null ? this.imgOriginal : this.imgEdited), this.trackBarBrightness.Value - this.trackBarVal);
                this.trackBarVal = this.trackBarBrightness.Value;
                this.picBoxEdit.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error!! in Brightness");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_BrightnessScreen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_BrightnessScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }

        /// <summary>
        /// Handles the Click event of the btnApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnApply_Click(object sender, EventArgs e)
        {
            try
            {
                this.trackBarBrightness.Value = int.Parse(this.txtBoxTrackBarVal.Text);
                this.imgEdited = IMG_Processor.IMG_HistogramOp.IMG_BrightnessBuffer((this.imgEdited == null ? this.imgOriginal : this.imgEdited), this.trackBarBrightness.Value - this.trackBarVal);
                this.trackBarVal = this.trackBarBrightness.Value;
                this.picBoxEdit.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error!! in brightness");
            }
        }
    }
}
